package com.apobates.jforum2.troll.accounts.biz.impl;

import com.apobates.jforum2.troll.accounts.biz.MemberActiveRecordsService;
import com.apobates.jforum2.troll.accounts.biz.dao.MemberActiveRecordsDao;
import com.apobates.jforum2.troll.accounts.entity.MemberActiveRecords;
import com.apobates.jforum2.troll.utils.persistence.Page;
import com.apobates.jforum2.troll.utils.persistence.Pageable;
import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.TreeMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import io.seata.spring.annotation.GlobalTransactional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author xiaofanku
 * @since 20200515
 */
@Service
public class MemberActiveRecordsServiceImpl implements MemberActiveRecordsService{
    @Autowired
    private MemberActiveRecordsDao memberActiveRecordsDao;
    private final static Logger logger = LoggerFactory.getLogger(MemberActiveRecordsServiceImpl.class);
    
    @Override
    public Page<MemberActiveRecords> getByMember(long memberId, Pageable pageable) {
        return memberActiveRecordsDao.findAllByMember(memberId, pageable);
    }
    
    @Override
    public Page<MemberActiveRecords> getAll(Pageable pageable) {
        return memberActiveRecordsDao.findAll(pageable);
    }
    
    @Override
    public Stream<MemberActiveRecords> getByMember(long memberId, String memberNames, int showSize) {
        return memberActiveRecordsDao.findAllByMember(memberId, memberNames, showSize);
    }

    @Override
    public Stream<MemberActiveRecords> getByMemberByNames(String memberNames, int showSize) {
        return memberActiveRecordsDao.findAllByMemberNames(memberNames, showSize);
    }

    @Override
    public Optional<MemberActiveRecords> get(long id) {
        return memberActiveRecordsDao.findOne(id);
    }
    
    @Override
    public Optional<MemberActiveRecords> getPreviousLoginRecord(long memberId, String memberNames) {
        logger.error("[account-api][SRV] start query last active");
        try{
            List<MemberActiveRecords> rs = memberActiveRecordsDao
                    .findAllLoginAction(memberId, memberNames, 2)
                    .sorted(Comparator.comparing(MemberActiveRecords::getActiveDateTime).reversed())
                    .collect(Collectors.toList());
            logger.error("[account-api][SRV] query last active size:"+rs.size());
            return Optional.ofNullable(rs.get(1));
        }catch(Exception e){
            logger.error("[account-api][SRV] query last active fail, exception:"+e.getMessage());
            if(logger.isDebugEnabled()){
                logger.debug(e.getMessage(), e);
            }
        }
        return Optional.empty();
    }

    @Transactional
    @GlobalTransactional
    @Override
    public long create(MemberActiveRecords entity) {
        try {
            memberActiveRecordsDao.save(entity);
            return entity.getId();
        } catch (Exception e) {
            if(logger.isDebugEnabled()){
                logger.debug("创建会员活跃记录失败", e);
            }
        }
        return 0L;
    }
    
    @Override
    public Map<String, Long> statsDevice() {
        return memberActiveRecordsDao.groupForDevice();
    }
    
    @Override
    public Map<String, Long> statsIsp() {
        return memberActiveRecordsDao.groupForISP();
    }
    
    @Override
    public Map<String, Long> statsProvince() {
        return memberActiveRecordsDao.groupForProvince();
    }
    
    @Override
    public TreeMap<String, Long> groupMemberForActivity(LocalDateTime start, LocalDateTime finish) {
        return memberActiveRecordsDao.groupMemberForActivity(start, finish);
    }
}